<?php

namespace app\admin\model;

use app\admin\validate\Person;
use app\admin\struct\PersonStruct;
use think\facade\Db;
use think\Model;
use think\model\concern\SoftDelete;
use think\model\relation\HasOne;
use think\helper\Arr;

class StaffModel extends Common
{
    use PersonStruct;

    use SoftDelete;

    // 设置字段信息
    protected $schema = [

    ];

    /**
     * 类型转换
     * @var string[]
     */
    protected $type = [

    ];


    /**
     * @var string
     */
    protected $name = 'shop_person';
    protected $deleteTime = 'delete_time';
    protected $defaultSoftDelete = 0;
    protected $autoWriteTimestamp = true;


    /**
     * 驼峰格式转换
     * @var bool
     */
    protected $convertNameToCamel = true;
    protected $append = ['achieveAmt', 'nursNumber'];


    /**
     * 获取门店员工列表
     */
    public function getStaffAdminListResource(array $shopIds, int $page, int $limit, $request)
    {
        // $where = $this->getPersonCondition($request);
        $name = $request['staffName'] ?? '';
        $shop_id = $request['shop_id'] ?? '';
        

        $where = [];

        if($name){
            array_push($where,['name','like',"$name%"]);
        }

        if($shop_id){
            array_push($where,['shop_id','=',$shop_id]);
        }
        
		array_push($where,['status','=',1]);

        $connect = $this->where($where)


            ->when($shopIds ?? [], function ($query) use ($shopIds) {


                return $query->whereIn('shop_id', $shopIds);


            });


        $count =$connect->count();


        $list =$connect->page($page, $limit)->select();


        return [


            'list' => $list,


            'dataCount' => $count


        ];
    }

    /**


     * @return array


     * @author ran


     * @date 2021-10-16 16:54


     * mailbox 466180170@qq.com


     */


    private function getPersonCondition(array $request)


    {


        $where = [];


        $shopId = isset($request['shopId']) && intval($request['shopId']) ? intval($request['shopId']) : 0;


        $name = isset($request['name']) && trim($request['name']) ? trim($request['name']) : '';


        $shopId && array_push($where, ['shop_id', '=', $shopId]);


        $phone = isset($request['phone']) && trim($request['phone']) ? trim($request['phone']) : '';


        $positionId = isset($request['positionId']) && intval($request['positionId']) ? intval($request['positionId']) : 0;


        $status = isset($request['status']) && intval($request['status']) ? intval($request['status']) : 0;


        $name && array_push($where, ['name', 'like', '%' . $name . '%']);


        $phone && array_push($where, ['phone', '=', $phone]);


        $positionId && array_push($where, ['position_id', '=', $positionId]);


        $status && array_push($where, ['status', '=', $status]);


        return $where;


    }

    
    
}